﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_QUANTRIHETHONG
{
    public partial class frm_main : Form
    {
        public Boolean SWITCHUSER = false;
        hethongDataContext context = cls_quantrihethong.getcontext();
        //daivietDataContext context = new daivietDataContext();
        
        private string tendangnhap;
        public string _tendangnhap
        {
            get { return tendangnhap;}
            set {
                tendangnhap = value;
            }
        }
        public frm_main()
        {
            InitializeComponent();
        }

        private void frm_main_Load(object sender, EventArgs e)
        {
            try
            {
            IEnumerable<DM_nhanvien> nhanviens=from c in context.DM_nhanviens where c.username.Equals(tendangnhap) select c;
            if (nhanviens.Count() > 0)
            {
                DM_nhanvien nhanvien = nhanviens.ElementAt(0);
                if (nhanvien.trangthai == true)
                {
                    loadmenu(e, tendangnhap);
                    this.tsstennhanvien.Text = nhanviens.ElementAt(0).hoten.ToUpper();
                    DateTime serverdate = context.p_getdate().ElementAt(0).serverdatetime;
                    string ngaylamviec = "";
                    if (serverdate.Day < 10)
                    {
                        ngaylamviec = ngaylamviec + "0" + serverdate.Day + "/";
                    }
                    else
                    {
                        ngaylamviec = ngaylamviec + serverdate.Day + "/";
                    }
                    if (serverdate.Month < 10)
                    {
                        ngaylamviec = ngaylamviec + "0" + serverdate.Month + "/";
                    }
                    else
                    {
                        ngaylamviec = ngaylamviec + serverdate.Month + "/";
                    }
                    ngaylamviec = ngaylamviec + serverdate.Year;

                    this.tssngaylamviec.Text = ngaylamviec;
                }
            }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        public void loadmenu(EventArgs e,string tendangnhap)
        {
            string manv;
            IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.username.Equals(tendangnhap) select c;
            manv = nhanviens.ElementAt(0).manv;
            IEnumerable<HT_menu> list = from c in context.HT_menus where c.cap.Equals(1) select c;
            //IEnumerable<HT_menu_nhanvien> list = from c in context.HT_menu_nhanviens join d in context.HT_menus on c.mamenu equals d.mamenu    

            HT_menu mn;

            for (int i = 0; i < list.Count(); i++)
            {
                mn = list.ElementAt(i);
                IEnumerable<HT_menu_nhanvien> menunhanviens = from c in context.HT_menu_nhanviens where c.HT_menu.mamenucha.Equals(mn.mamenu) where c.manv.Equals(manv) select c;
                ToolStripMenuItem parent = new ToolStripMenuItem();
                parent.Text = mn.tenmenu;
                parent.Name = mn.mamenu;
                if (menunhanviens.Count() > 0)
                {

                    //ToolStripItem item;
                    //item.Text = mn.tenmenu;
                    //item.Name = mn.mamenu;
                     
                    

                    //this.mn_mainmenu.Items[i].Name = mn.mamenu;
                    //IEnumerable<HT_menu> childlist = from c in context.HT_menus where c.cap.Equals(2) where c.mamenucha.Equals(mn.mamenu) select c;

                    IEnumerable<HT_menu> childlist = from c in context.HT_menu_nhanviens join d in context.HT_menus on c.mamenu equals d.mamenu where d.mamenucha.Equals(mn.mamenu) where d.cap.Equals(2) where c.manv.Equals(manv) select d;

                    HT_menu cmn;

                    for (int j = 0; j < childlist.Count(); j++)
                    {
                        cmn = childlist.ElementAt(j);
                        ToolStripMenuItem child = new ToolStripMenuItem();
                        child.Name = cmn.mamenu;
                        child.Text = cmn.tenmenu;




                        child.Click += new System.EventHandler(sub_mainmenu_Click);

                        parent.DropDownItems.Add(child);

                    }
                   

                }
                if (i == 0)
                {
                    ToolStripMenuItem switchuser = new ToolStripMenuItem();
                    switchuser.Name = constant.SWITCHUSER;
                    switchuser.Text = constant.SWITCHUSER;
                    switchuser.Click += new System.EventHandler(sub_mainmenu_Click);
                    parent.DropDownItems.Add(switchuser);
                    ToolStripMenuItem exit = new ToolStripMenuItem();
                    exit = new ToolStripMenuItem();
                    exit.Name = constant.EXIT;
                    exit.Text = constant.EXIT;
                    exit.Click += new System.EventHandler(sub_mainmenu_Click);
                    parent.DropDownItems.Add(exit);
                }
                this.mn_mainmenu.Items.Add(parent);

            }






        }

        private void mn_mainmenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            //this.label1.Text = e.ClickedItem.Name;
           
           
            //MessageBox.Show(e.ClickedItem.Name);    

        }
        private void sub_mainmenu_Click(Object sender, EventArgs e)
        {
            
            //MessageBox.Show(sender.ToString());
            string tenmenu = sender.ToString();
            if (tenmenu ==constant.SWITCHUSER)
            {
                SWITCHUSER = true;
                MODULE_QUANTRIHETHONG.frm_startup frm = new MODULE_QUANTRIHETHONG.frm_startup();

                frm.Show();
                this.Close();
                MODULE_QUANTRIHETHONG.frm_login frmlg = new MODULE_QUANTRIHETHONG.frm_login();
                frmlg.Show();
                frmlg.MdiParent = frm;
 
            }
            if (tenmenu == constant.EXIT)
            {
                Application.Exit();
            }
            IEnumerable<HT_menu> l=from c in context.HT_menus where c.tenmenu.Equals(tenmenu)select c;
            if (l.Count() > 0)
            {
                HT_menu m = l.ElementAt(0);
                string modulename, formname;
                modulename = m.module;
                formname = m.form;
                
                switch (modulename)
                { 
                    case "MODULE_DANHMUC":
                        MODULE_DANHMUC.cls_danhmuc clsdm = new MODULE_DANHMUC.cls_danhmuc();
                        clsdm.parent = this;
                        clsdm.ShowForm(formname);
                        break;
                    case "MODULE_KCS":
                        MODULE_KCS.cls_kcs clskcs = new MODULE_KCS.cls_kcs();
                        clskcs.parent = this;
                        clskcs.ShowForm(formname);
                        break;
                    case "MODULE_KETOAN":
                        MODULE_KETOAN.cls_ketoan clskt = new MODULE_KETOAN.cls_ketoan();
                        clskt.parent = this;
                        clskt.ShowForm(formname);

                        break;
                    case "MODULE_NHAPXUAT":
                        MODULE_NHAPXUAT.cls_nhapxuat clsnx = new MODULE_NHAPXUAT.cls_nhapxuat();
                        clsnx.parent = this;
                        clsnx.ShowForm(formname);
                        break;

                    case "MODULE_QUANTRIHETHONG":
                        MODULE_QUANTRIHETHONG.cls_quantrihethong clsqtht = new MODULE_QUANTRIHETHONG.cls_quantrihethong();
                        clsqtht.parent = this;
                        clsqtht.ShowForm(formname);
                        break;
                }
               // MessageBox.Show(m.form); 

            }

            //MessageBox.Show(e.ToString());
           
            
        }

        private void reportDocument1_InitReport(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
            cls.ShowForm("frm_report");
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            cls_quantrihethong cls = new cls_quantrihethong();
            cls.ShowForm("frm_thamso");
        }

        private void frm_main_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (SWITCHUSER == false)
            {
                Application.Exit();
            }
        }

       
    }
}
